package gameObject
{
	import away3d.containers.ObjectContainer3D;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.Responder;
	
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.RobotResource;
	import gameObject.resources3D.WeaponConnector;
	import gameObject.resources3D.WeaponResource;

	/**
	 * 战斗中玩家的低模机体
	 */	
	public class Robot extends GameObjectBase
	{
		private var _uid:int;
		private var _pid:int;
		private var _wep_m:Vector.<int> = new Vector.<int>;
		private var _wep_s:Vector.<int> = new Vector.<int>;
		
		private var _loadedResource:int = 0;
		private var _totalResource:int = 1;
		///////////////////////////////////////////////////////
		private var _hp:int;
		private var _maxHp:int;
		private var _dur:int;
		private var _maxDur:int;
		private var _def:int;
		private var _speed:Number;
		
		///////////////////////////////////////////////////////收集武器方便控制器调用
		
		private var _mainWeapons:Vector.<WeaponMain> = new Vector.<WeaponMain>;
		private var _secWeapons:Vector.<WeaponSec> = new Vector.<WeaponSec>;
		
		private var _hitArea:Circle;
		
		private var _root:ObjectContainer3D;
		///////////////////////////////////////////////////////

		public function get root():ObjectContainer3D
		{
			return _root;
		}

		public function get wep_m():Vector.<int>
		{
			return _wep_m;
		}

		public function get uid():int
		{
			return _uid;
		}
		
		public function get speed():Number
		{
			return _speed;
		}
		
		public function get hitArea():Circle
		{
			return _hitArea;
		}
		
		public function get mainWeapons():Vector.<WeaponMain>
		{
			return _mainWeapons;
		}
		
		public function get secWeapons():Vector.<WeaponSec>
		{
			return _secWeapons;
		}
		
		public function Robot(uid:int)
		{
			_uid = uid;
			_root = new ObjectContainer3D;
			addChild(_root);
			Global.phpServer.call("robotServer.getCurrentEquip", new Responder(gotEquipData),_uid);//取出装备品编号
		}

		private function gotEquipData(obj:Object):void//收到装备品编号
		{
			_pid = obj.pid;
			
			var str:String = obj.wep_m;
			var strAry:Array = str.split(",");
			for each(var p:String in strAry)_wep_m.push(int(p));
			
			str = obj.wep_s;
			strAry = str.split(",");
			for each(p in strAry)_wep_s.push(int(p));
			
			Global.phpServer.call("robotServer.getRobot", new Responder(gotValue),_pid);//取出装备机甲的参数
		}
		
		private function gotValue(obj:Object):void//收到装备机甲的参数
		{
			//设置机甲的初始参数
			_hp = obj.hp;
			_maxHp = obj.maxhp;
			_dur = obj.dur;
			_maxDur = obj.maxdur;
			_def = obj.def;
			_speed = obj.speed;
			
			_hitArea = new Circle(0xff00,obj.radius);
			
			//取出机甲的模型资源
			_resource = new RobotResource(obj.id,GameMesh.LOD_LOW);
			_resource.addEventListener(Event.COMPLETE,onRobResLoaded);
			_resource.load();
		}
		
		private function onRobResLoaded(e:Event):void//收到机甲的模型资源
		{
			_resource.removeEventListener(Event.COMPLETE,onRobResLoaded);
			
			for(var i:int=0;i<_wep_m.length;i++)//取出装备的主武器
			{
				if(_wep_m[i]>0)
				{
					_totalResource++;
					var mainWep:WeaponMain = new WeaponMain(_wep_m[i]);
					_mainWeapons.push(mainWep);
					mainWep.addEventListener(Event.COMPLETE,gotMainWeapon);
				}
			}
			
			for(i=0;i<_wep_s.length;i++)//取出装备的副武器
			{
				if(_wep_s[i]>0)
				{
					_totalResource++;
					var secWep:WeaponSec = new WeaponSec(_wep_s[i]);
					_secWeapons.push(secWep);
					secWep.addEventListener(Event.COMPLETE,gotSecWeapon);
				}
			}
			
			_loadedResource++;
			if(_loadedResource==_totalResource)loadedCompleted();
		}
		
		private function gotMainWeapon(e:Event):void//收到装备的主武器
		{
			var mainWep:WeaponMain = e.currentTarget as WeaponMain;
			mainWep.removeEventListener(Event.COMPLETE,gotMainWeapon);
			
			var robRes:RobotResource = _resource as RobotResource;
			var conID:int = _wep_m.indexOf(mainWep.pid);
			var con:WeaponConnector = robRes.getWeaponConnector(1,conID);
			
			con.install(mainWep.resource as WeaponResource);
			
			_loadedResource++;
			if(_loadedResource==_totalResource)loadedCompleted();
		}
		
		private function gotSecWeapon(e:Event):void//收到装备的副武器
		{
			var secWep:WeaponSec = e.currentTarget as WeaponSec;
			secWep.removeEventListener(Event.COMPLETE,gotSecWeapon);
			
			var robRes:RobotResource = _resource as RobotResource;
			var conID:int = _wep_s.indexOf(secWep.pid);
			var con:WeaponConnector = robRes.getWeaponConnector(2,conID);
			
			con.install(secWep.resource as WeaponResource);
			
			_loadedResource++;
			if(_loadedResource==_totalResource)loadedCompleted();
		}
		
		private function loadedCompleted():void//加载完成后显示模型并发出通知
		{
			_root.addChild(_resource);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
	}
}